﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Module.MailingSystem.Interfaces;
using Core.Module.Interfaces.Model;
using System.Net.Mail;
using umbraco.BusinessLogic;
using Core.Module.Interfaces.Controller;
using Core.Module.Interfaces;
using Core.Module;

namespace Module.MailingSystem.Mail
{
    public class MailController : IMailController
    {
        public IMailTemplate GetTemplate(IModuleNode forPage, string templateFieldName, Dictionary<string, string> mailReplacements)
        {
            IMailingModule module= MailingModule(forPage);
            if (module == null) return null;
            IMailTemplate resultTemplate = Convert(module.GetTemplateNode(forPage, templateFieldName));
            if (resultTemplate == null) return null;
            resultTemplate.Properties = mailReplacements;
            return module.ReplaceTemplateParamethers(forPage, resultTemplate);
        }
    

        public bool Send(IModuleNode forPage, IMailTemplate template)
        {
            IMailingModule module = MailingModule(forPage);
            return module == null ? false: module.Send(forPage, template);
        }
        public bool Send(int forPage, string templateFieldAliasOrTemplateName, Dictionary<string, string> mailReplacements)
        {
            IModuleNode node=Context.Frontend.Nodes.Get(forPage.ToString());            
            IMailTemplate template = GetTemplate(node, templateFieldAliasOrTemplateName, mailReplacements);
            return Send(node, template);
        }

        public MailMessage Convert(IModuleNode forPage, IMailTemplate templateMessage)
        {
            IMailingModule module= MailingModule(forPage);
            return module==null?null:module
                   .Convert(forPage, templateMessage);
        }

        public IMailTemplate Convert(IModuleNode templateNode)
        {
            if (templateNode == null) return null;

            IMailTemplate result=ObjectManager.Resolve<IMailTemplate>();
            INodeController<IFrontend> nc=Context.Frontend.Nodes; 
            result.Node = templateNode;
            
            result.Subject = nc.GetPropertyValue(result.Node, "emailSubject") as string;
            result.Head= nc.GetPropertyValue(result.Node, "emailHead") as string;
            result.Template = nc.GetPropertyValue(result.Node, "emailTemplate") as string;
            result.CC = nc.GetPropertyValue(result.Node, "emailCC") as string;
            result.BCC = nc.GetPropertyValue(result.Node, "emailBCC") as string;
            result.IsPlainText = nc.GetPropertyValue(result.Node, "shouldBeSendInPlainTextFormat") as string;
            result.ServerName = nc.GetPropertyValue(result.Node, "serverName") as string;
            result.From = nc.GetPropertyValue(result.Node, "sendFrom") as string;
            result.DeliveryNotificationOptions = nc.GetPropertyValue(result.Node, "notificationOptions") as string;
            result.Priority = nc.GetPropertyValue(result.Node, "messagePriority") as string;
            result.To = nc.GetPropertyValue(result.Node, "sendTo") as string;
            
            return result;
        }
        
        #region internal methods
        
        private T LogErrorAndReturn<T>(string message,LogTypes ltype, T result)
        {
            Log.Add(ltype, -1, message);
            return result;
        }
          
        #endregion

        public IMailingModule MailingModule(IModuleNode forPage)
        {
                ISite pageSite = Context.Frontend.Sites.Get(forPage.SiteId);
                if (pageSite == null) return LogErrorAndReturn<IMailingModule>("can't return mailing module, can't determinate site for page:" + forPage.Id, LogTypes.Error, null);
                IMailingModule  result=Context.Frontend.Modules.Get(pageSite, "MailingModule") as IMailingModule;
                if (result == null) return LogErrorAndReturn<IMailingModule>("No one module is installed and active for site:"+pageSite.Node.Name+" which can be used to send mails [alias:MailingModule]", LogTypes.Error, null);
                return result;
        }
    }
}
